package com.devsoft.foodfrenzy;
//this class is for the main menu scene; it includes the 4 buttons and background

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;

import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class MainMenuScene extends BaseScene implements IOnMenuItemClickListener{
	
	private MenuScene menuChildScene;	//menu scene is imported
	private final int MENU_SP = 0;
	private final int MENU_MP = 1;
	private final int MENU_OPTIONS = 2;
	private final int MENU_UNLOCKS = 3;
	private final int MENU_PLAYERINFO = 4;
	private final int MENU_CREDITS = 5;
	
	private void createMenuChildScene()
	{
	    menuChildScene = new MenuScene(camera);	//sets up the palette for the screen
	    menuChildScene.setPosition(400, 240);
	    
	    final IMenuItem SPMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_SP, resourcesManager.singleplayer_region, vbom), 1.2f, 1);	//creates buttons
	    final IMenuItem MPMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_MP, resourcesManager.multiplayer_region, vbom), 1.2f, 1);
	    final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_OPTIONS, resourcesManager.options_region, vbom), 1.2f, 1);
	    final IMenuItem unlocksMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_UNLOCKS, resourcesManager.achievements_region, vbom), 1.2f, 1);
	    final IMenuItem infoMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAYERINFO, resourcesManager.info_region, vbom), 1.2f, 1);
	    final IMenuItem creditsMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_CREDITS, resourcesManager.credits_region, vbom), 1.2f, 1);
	    
	    menuChildScene.addMenuItem(SPMenuItem);	//adds buttons to the menu
	    menuChildScene.addMenuItem(MPMenuItem);
	    menuChildScene.addMenuItem(optionsMenuItem);
	    menuChildScene.addMenuItem(unlocksMenuItem);
	    menuChildScene.addMenuItem(infoMenuItem);
	    menuChildScene.addMenuItem(creditsMenuItem);
	    
	    menuChildScene.buildAnimations();		//builds animation for button press; makes button bigger when pressed
	    menuChildScene.setBackgroundEnabled(false);
	    
	    SPMenuItem.setPosition(SPMenuItem.getX() - 250, SPMenuItem.getY() - 275);	//sets button locations; need to resituate
	    MPMenuItem.setPosition(MPMenuItem.getX() - 250, MPMenuItem.getY() - 280);
	    optionsMenuItem.setPosition(optionsMenuItem.getX() - 250, optionsMenuItem.getY() - 285);
	    unlocksMenuItem.setPosition(unlocksMenuItem.getX() - 250, unlocksMenuItem.getY() - 290);
	    infoMenuItem.setPosition(infoMenuItem.getX() - 250, infoMenuItem.getY() - 295);
	    creditsMenuItem.setPosition(creditsMenuItem.getX() - 250, creditsMenuItem.getY() - 300);
	    
	    menuChildScene.setOnMenuItemClickListener(this);	//listen for press, act appropriately
	    
	    setChildScene(menuChildScene);
	    
	}
	
	
	@Override
	public void createScene() 
	{
		//UserData.getInstance().init(activity.getBaseContext());
		createBackground();
	    createMenuChildScene();
	    if(!(UserData.getInstance().getMusicPref())){
			resourcesManager.main_menu_music.play();
		}
	}

	@Override
	public void onBackKeyPressed() 	//exit app when backed
	{
		System.exit(0);
		//SceneManager.getInstance().loadTitleScene(engine);
		
	}
	

	@Override
	public SceneType getSceneType()
	{
	    return SceneType.SCENE_MAINMENU;
	    
	}
	
	
	@Override
	public void disposeScene() 
	{
		menuChildScene.detachSelf();
		menuChildScene.dispose();
        this.detachSelf();
        this.dispose();
		
	}
	
	
	private void createBackground()
	{
	    attachChild(new Sprite(400, 240, resourcesManager.menu_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });
	    
	}
	
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		switch(pMenuItem.getID())
	    {
			case MENU_SP:
				SceneManager.getInstance().loadSPMenuScene(engine);
	            return true;
			case MENU_MP:
				SceneManager.getInstance().loadMultiplayerGameScene(engine);
				return true;
	        case MENU_OPTIONS:
	        	SceneManager.getInstance().loadOptionsScene(engine);
	        	return true;
	        case MENU_UNLOCKS:
	        	SceneManager.getInstance().loadAchievementsScene(engine);
	        	return true;
	        case MENU_PLAYERINFO:
	        	SceneManager.getInstance().loadPlayerInfoScene(engine);
	        	return true;
	        case MENU_CREDITS:
	        	SceneManager.getInstance().loadCreditsScene(engine);
	        	return true;
	        default:
	            return false;
	            
	    }
		
	}

}
